package com.yan.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yan.jackson.Student;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author taoge
 * @Date 2024/8/13 14:33
 * @Description
 */
public class JsonDemo2 {
    public static void main(String[] args) {
        Student stu = new Student("yft", 18, new Date());
        String jsonStu = JSON.toJSONString(stu);
        System.out.println(jsonStu);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(stu);
        System.out.println("jsonObject = " + jsonObject);
        Set<String> keySet = jsonObject.keySet();
        String name = jsonObject.getString("name");
        Integer age = jsonObject.getInteger("age");
        Date birthday = jsonObject.getDate("birthday");
        String birthdayStr = new SimpleDateFormat("yyyy-MM-ddd").format(birthday);
        System.out.println(name + "--" + age + "--" + birthdayStr);

        List<Student> list = new LinkedList<>();
        list.add(new Student("hpy" , 18 , new Date(0L)));
        list.add(new Student("cqp" , 19 , new Date(110L)));
        list.add(new Student("wl" , 20 , new Date(11110L)));

        String s = JSON.toJSONString(list);
        System.out.println(s);

        JSONArray jsonArray = (JSONArray) JSON.toJSON(list);
        for (Object obj : jsonArray) {
            JSONObject object = (JSONObject) obj;
            String name1 = object.getString("name");
            Integer age1 = object.getInteger("age");
            Date birthday1 = object.getDate("birthday");
            System.out.println(name1 + "--" + age1 + "--" + birthday1);
        }
    }
}
